# coding=utf-8
import requests,urllib2,threading,time,os
from bs4 import BeautifulSoup

url = "http://1024.stv919.net/pw/thread.php?fid=14"

all_pic=0

headers = {
	##'Host': '1024.stv919.net',
	'Connection': 'keep-alive',
	'Cache-Control': 'max-age=0',
	'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
	#  'X-Requested-With': 'XMLHttpRequest',
	'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36',
	# 'DNT': '1',
	## 'Accept-Encoding': 'gzip, deflate',
	'Accept-Language': 'zh-CN,zh;q=0.8,ja;q=0.6',
	'Referer': 'http://1024.stv919.net/pw/thread.php?fid=14'
}

base_url = 'http://1024.stv919.net/pw/'
url2 = 'http://1024.stv919.net/pw/htm_data/14/1707/702462.html'


##url='http://1024.stv919.net/pw/thread.php?fid=14'

def run_page(urlitem, conn, path):
	print urlitem
	#conn = requests.session()
	rsp = conn.get(url=urlitem, headers=headers)
	print rsp.status_code
	try:
		html = rsp.content
		h = BeautifulSoup(html, "html.parser")
		items = h.select('.tpc_content img')
		pic_num = len(items)
		index = 0
		f=open(os.getcwd() + "a.log", 'a+')
		for i in items:
			##pic = conn.get(url=i['src'],headers=headers)
			name = str(long(int(round(time.time() * 1000))))
			f.writelines(i['src']+'\n')

			index += 1
			print i['src'] + ' is ok,%d,%s,%s ' % (
				index, threading.currentThread().name, threading.currentThread().name)
		f.close()
		# urllib.urlretrieve(i['src'], 'C://WORKSPACE//logs//' + str(long(int(round(time.time() * 1000)))) + ".gzip")
	except urllib2.HTTPError, e:
		print e.code, e.reason
	conn.close()
	print '# all is ok %d' % (pic_num)
	return pic_num


print '###################################'
conn = requests.session()
rsq = conn.get(url, headers=headers)
print rsq.status_code
html = rsq.content

dom = BeautifulSoup(html, 'html.parser')
items = dom.select('h3 a')

for item in items[3:]:
	#print base_url + item['href']
	all_pic+=run_page(base_url + item['href'], conn, "c://WORKSPACE//logs//")

conn.close()


print 'all %d' %(all_pic)